<?php

namespace iBrand\Shop\Server\Controllers\V2;

use iBrand\Common\Controllers\Controller;
use iBrand\Component\Order\Models\Adjustment;
use iBrand\Component\Order\Models\Order;
use iBrand\Component\Order\Repositories\OrderRepository;
use iBrand\Component\Payment\Models\Payment;
use iBrand\Component\User\Repository\UserRepository;
use iBrand\Shop\Core\Models\PaymentDetail;
use iBrand\Shop\Core\Repositories\ShopOrderRepository;
use iBrand\Shop\Server\Resources\OrderResource;

class OrderController extends Controller
{
	protected $orderRepository;
	protected $userRepository;
	protected $shopOrderRepository;

	public function __construct(OrderRepository $orderRepository, UserRepository $userRepository, ShopOrderRepository $shopOrderRepository)
	{
		$this->orderRepository     = $orderRepository;
		$this->userRepository      = $userRepository;
		$this->shopOrderRepository = $shopOrderRepository;
	}

	public function index()
	{
		$clerk               = auth('clerk')->user();
		$limit               = request('limit') ? request('limit') : 15;
		$where['type']       = Order::TYPE_SHOP;
		$where['channel']    = 'shop';
		$where['channel_id'] = $clerk->shop_id;
		$where['status']     = Order::STATUS_PAY;

		if (request('order_no')) {
			$where['order_no'] = ['order_no', 'like', '%' . request('order_no') . '%'];
			unset($where['channel_id']);
		}

		if (request('user_id')) {
			$where['user_id'] = request('user_id');
		}

		$list = $this->orderRepository->getOrdersByConditions($where, [], $limit, ['user']);

		return $this->paginator($list, OrderResource::class);
	}

	public function detail($order_no)
	{
		$order = $this->orderRepository->with('items')->findWhere(['order_no' => $order_no, 'status' => Order::STATUS_PAY])->first();
		if (!$order) {
			return $this->failed('订单不存在');
		}

		$user          = $this->userRepository->find($order->user_id);
		$payments      = $order->payments()->where('order_type', Order::class)->where('status', Payment::STATUS_COMPLETED)->get();
		$shopOrder     = $this->shopOrderRepository->with('clerk')->findWhere(['order_id' => $order->id])->first();
		$paymentDetail = PaymentDetail::where('order_id', $order->id)->first();

		$pointAdjustment = $order->adjustments()->where('type', Adjustment::ORDER_POINT_DISCOUNT_ADJUSTMENT)->first();
		$point_amount    = 0;
		if ($pointAdjustment) {
			$point_amount = number_format(abs($pointAdjustment->amount) / 100, 2, '.', '');
		}

		$couponAdjustment = $order->adjustments()->where('type', Adjustment::ORDER_DISCOUNT_ADJUSTMENT)->where('origin_type', 'coupon')->first();
		$coupon_amount    = 0;
		if ($couponAdjustment) {
			$coupon_amount = number_format(abs($couponAdjustment->amount) / 100, 2, '.', '');
		}

		$userOrderDiscount     = $order->adjustments()->where('type','<>', Adjustment::ORDER_WIPE_DISCOUNT_ADJUSTMENT)->sum('amount');
     
		$order_discount_amount = 0;
		if ($userOrderDiscount) {
			$order_discount_amount = number_format(abs($userOrderDiscount) / 100, 2, '.', '');
		}

		return $this->success([
			'order'         => $order,
			'payments'      => $payments,
			'user'          => $user,
			'shopOrder'     => $shopOrder,
			'paymentDetail' => $paymentDetail,
			'adjustments'   => [
				'point_amount'          => $point_amount,
				'coupon_amount'         => $coupon_amount,
				'order_discount_amount' => $order_discount_amount,
			],
		]);
	}
}